package com.cwj.we.module.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.cwj.we.R;
import com.cwj.we.base.BaseFragment;
import com.cwj.we.base.BaseRVAdapter;
import com.cwj.we.base.BaseRVHolder;
import com.cwj.we.bean.BiZhiBean;
import com.cwj.we.bean.GetDataType;
import com.cwj.we.bean.WallpaperBiZhiBean;
import com.cwj.we.module.bizhi.PhotoView2Activity;
import com.cwj.we.utils.LoadingDialog;
import com.cwj.we.utils.ToastUtil;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;

/**
 * 壁纸首页
 */
public class BiZhiFragment extends BaseFragment<VerticalPrensenter> implements VerticalView {

    @BindView(R.id.srl)
    SmartRefreshLayout srl;
    @BindView(R.id.rv)
    RecyclerView rv;
    private String mTitle, id;
    private LoadingDialog loadingDialog;

    private BaseRVAdapter<BiZhiBean.ResBean.VerticalBean> adapter;
    private BaseRVAdapter<WallpaperBiZhiBean.ResBean.WallpaperBean> adapter2;
    private int limit = 20;
    private int skip = 1;

    public static BiZhiFragment getInstance(String title, String id) {
        BiZhiFragment bf = new BiZhiFragment();
        bf.mTitle = title;
        bf.id = id;
        return bf;
    }

    @Override
    protected VerticalPrensenter createPresenter() {
        return new VerticalPrensenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_bi_zhi;
    }

    @Override
    protected void initView() {
        ImmersionBar.with(getActivity())
                .statusBarDarkFont(true)  //状态栏字体是深色，不写默认为亮色
                .init();
        loadingDialog = new LoadingDialog(getActivity(), "加载中...");
    }

    @Override
    protected void initData() {
        loadingDialog.show();

        if ("最新壁纸".equals(mTitle.trim())) {
            presenter.getNewBizhi(id, limit, skip, GetDataType.GETDATA);

            adapter = new BaseRVAdapter<BiZhiBean.ResBean.VerticalBean>(R.layout.item_bizhi) {
                @Override
                public void onBindVH(BaseRVHolder holder, BiZhiBean.ResBean.VerticalBean data, int position) {
                    ImageView iv = holder.getView(R.id.iv);
                    Glide.with(getActivity())
                            .load(data.getThumb())
                            .into(iv);

                    iv.setOnClickListener(view -> {
                        Intent intent = new Intent(getActivity(), PhotoView2Activity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("dataBean", new Gson().toJson(adapter.getData()));
                        intent.putExtras(bundle);
                        intent.putExtra("currentPosition", position);
                        startActivity(intent);
                    });
                }
            };

            //下拉刷新
            srl.setOnRefreshListener(refreshLayout -> {
                skip = 1;
                presenter.getNewBizhi(id, limit, skip, GetDataType.REFRESH);
            });

            //上拉加载更多
            srl.setOnLoadMoreListener(refreshLayout -> {
                skip += limit;
                presenter.getNewBizhi(id, limit, skip, GetDataType.LOADMORE);
            });

            //设置适配器
            rv.setAdapter(adapter);
            rv.setLayoutManager(new GridLayoutManager(getActivity(), 2));//列数设置
        } else if ("热门壁纸".equals(mTitle.trim())) {
            presenter.getHotBizhi(id, limit, skip, GetDataType.GETDATA);

            adapter = new BaseRVAdapter<BiZhiBean.ResBean.VerticalBean>(R.layout.item_bizhi) {
                @Override
                public void onBindVH(BaseRVHolder holder, BiZhiBean.ResBean.VerticalBean data, int position) {
                    ImageView iv = holder.getView(R.id.iv);
                    Glide.with(getActivity())
                            .load(data.getThumb())
                            .into(iv);

                    iv.setOnClickListener(view -> {
                        Intent intent = new Intent(getActivity(), PhotoView2Activity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("dataBean", new Gson().toJson(adapter.getData()));
                        intent.putExtras(bundle);
                        intent.putExtra("currentPosition", position);
                        startActivity(intent);
                    });
                }
            };

            //下拉刷新
            srl.setOnRefreshListener(refreshLayout -> {
                skip = 1;
                presenter.getHotBizhi(id, limit, skip, GetDataType.REFRESH);
            });

            //上拉加载更多
            srl.setOnLoadMoreListener(refreshLayout -> {
                skip += limit;
                presenter.getHotBizhi(id, limit, skip, GetDataType.LOADMORE);
            });

            //设置适配器
            rv.setAdapter(adapter);
            rv.setLayoutManager(new GridLayoutManager(getActivity(), 2));//列数设置
        } else if ("电脑壁纸".equals(mTitle.trim())) {
            presenter.getWallpaperBizhi(id, limit, skip, GetDataType.GETDATA);

            adapter2 = new BaseRVAdapter<WallpaperBiZhiBean.ResBean.WallpaperBean>(R.layout.item_wallpaper) {
                @Override
                public void onBindVH(BaseRVHolder holder, WallpaperBiZhiBean.ResBean.WallpaperBean data, int position) {
                    ImageView iv = holder.getView(R.id.iv);
                    Glide.with(getActivity())
                            .load(data.getThumb())
                            .into(iv);
                }
            };

            adapter2.setOnItemClickListener((baseQuickAdapter, view, i) -> {
                Intent intent = new Intent(getActivity(), PhotoView2Activity.class);
                Bundle bundle = new Bundle();
                bundle.putString("dataBean", new Gson().toJson(adapter2.getData()));
                intent.putExtras(bundle);
                intent.putExtra("currentPosition", i);
                startActivity(intent);
            });

            //下拉刷新
            srl.setOnRefreshListener(refreshLayout -> {
                skip = 1;
                presenter.getWallpaperBizhi(id, limit, skip, GetDataType.REFRESH);
            });

            //上拉加载更多
            srl.setOnLoadMoreListener(refreshLayout -> {
                skip += limit;
                presenter.getWallpaperBizhi(id, limit, skip, GetDataType.LOADMORE);
            });

            //设置适配器
            rv.setAdapter(adapter2);
            rv.setLayoutManager(new GridLayoutManager(getActivity(), 2));//列数设置
        }
    }

    @Override
    public void onError(String msg) {
        loadingDialog.dismiss();
        ToastUtil.showTextToast(getActivity(), msg);
        srl.finishRefresh();
        srl.finishLoadMore();
    }

    @Override
    public void getNewBizhiSuccess(BiZhiBean bean, Integer type) {
        loadingDialog.dismiss();
        if (bean != null && bean.getRes() != null && bean.getRes().getVertical() != null) {
            switch (type) {
                case GetDataType.GETDATA://获取数据成功
                    adapter.setNewData(bean.getRes().getVertical());
                    break;
                case GetDataType.REFRESH://刷新成功
                    adapter.setNewData(bean.getRes().getVertical());
                    srl.finishRefresh();
                    srl.setNoMoreData(false);
                    break;
                case GetDataType.LOADMORE://加载成功
                    if (bean.getRes().getVertical() != null && !bean.getRes().getVertical().isEmpty()) {
                        adapter.addData(bean.getRes().getVertical());
                        if (bean.getRes().getVertical().size() < limit) {
                            srl.finishLoadMoreWithNoMoreData();
                        } else {
                            srl.finishLoadMore();
                        }
                    } else {
                        srl.finishLoadMoreWithNoMoreData();
                    }
                    break;
            }
        }
    }

    @Override
    public void getWallpaperBizhiSuccess(WallpaperBiZhiBean bean, Integer type) {
        loadingDialog.dismiss();
        if (bean != null && bean.getRes() != null && bean.getRes().getWallpaper() != null) {
            switch (type) {
                case GetDataType.GETDATA://获取数据成功
                    adapter2.setNewData(bean.getRes().getWallpaper());
                    break;
                case GetDataType.REFRESH://刷新成功
                    adapter2.setNewData(bean.getRes().getWallpaper());
                    srl.finishRefresh();
                    srl.setNoMoreData(false);
                    break;
                case GetDataType.LOADMORE://加载成功
                    if (bean.getRes().getWallpaper() != null && !bean.getRes().getWallpaper().isEmpty()) {
                        adapter2.addData(bean.getRes().getWallpaper());
                        if (bean.getRes().getWallpaper().size() < limit) {
                            srl.finishLoadMoreWithNoMoreData();
                        } else {
                            srl.finishLoadMore();
                        }
                    } else {
                        srl.finishLoadMoreWithNoMoreData();
                    }
                    break;
            }
        }
    }
}